package tq.com.tqteacher.activity.newPublish;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.githang.statusbar.StatusBarCompat;

import tq.com.systools.system.StringHelper;
import tq.com.tqcom.base.BaseActivity;
import tq.com.tqcom.net.INetCallback;
import tq.com.tqcom.net.TQNetResult;
import tq.com.tqcom.net.TQPictureUpload;
import tq.com.tqcom.util.TQLoadingHelper;
import tq.com.tqcom.view.TQHeader5View;
import tq.com.tqteacher.R;
import tq.com.tqteacher.activity.TQCropPictureActivity;
import tq.com.tqteacher.activity.captureVideo.TQCaptureActivity;
import tq.com.tqteacher.activity.playerView.TQPlayerActivity;
import tq.com.tqteacher.adapt.newPublish.TQVideoSetsAdapter;
import tq.com.tqteacher.model.TQAtachModel;
import tq.com.tqteacher.model.TQNewPingJiangPublish;
import tq.com.tqteacher.model.TQPublishType;
import tq.com.tqteacher.view.TQHeader3ViewHold;
import tq.com.upload.IUploadFileListener;
import tq.com.upload.TQUploadFileManager;

public class TQVideoSetsManager extends BaseActivity {
    private TextView nextView;
    private TQHeader5View mHeader5View;

    private RecyclerView mRecyclerView;
    private TQVideoSetsAdapter mTQVideoSetsAdapter;
    private TQAtachModel currentCapture;

    private TQNewPingJiangPublish mTQNewPingJiangPublish;


    static public void show(Activity pActivity){
        Intent intent = new Intent(pActivity, TQVideoSetsManager.class);
        pActivity.startActivity(intent);
    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setStatusBar();
        setContentView(R.layout.activity_fragment_layout);
        mHeader5View = findViewById(R.id.title);
        mHeader5View.getTitleView().setText("评讲");
        mHeader5View.getBackIcon().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TQVideoSetsManager.this.finish();
            }
        });
        nextView = findViewById(R.id.next);
        nextView.setText("完成");

        mRecyclerView = findViewById(R.id.content);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        if(mTQVideoSetsAdapter == null){
            mTQVideoSetsAdapter = new TQVideoSetsAdapter(this);
        }
        mRecyclerView.setAdapter(mTQVideoSetsAdapter);
        mTQNewPingJiangPublish = (TQNewPingJiangPublish) TQPublishType.getSharePublishItem();//(TQNewPingJiangPublish) getIntent().getSerializableExtra(TQPublishType.PUBLISH_INFO);

        if(mTQNewPingJiangPublish != null){
            mTQVideoSetsAdapter.repalceData(mTQNewPingJiangPublish.getAtachModels());
            mTQVideoSetsAdapter.setItemClickListener(mItemClickListener);
        }


        findViewById(R.id.next).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TQVideoSetsManager.this.finish();
            }
        });
        if(TQPublishType.getSharePublishItem().isPublish() && TQPublishType.getTQOpenFrom() == TQPublishType.TQOpenFrom.home){
            findViewById(R.id.next).setVisibility(View.GONE);
            mTQVideoSetsAdapter.setCanModify(false);
        }else{
            ((TextView)findViewById(R.id.next)).setText("完成");
        }
    }



    private TQVideoSetsAdapter.IItemClickListener mItemClickListener = new TQVideoSetsAdapter.IItemClickListener() {
        @Override
        public void clickCapture(TQAtachModel videoItem) {
            currentCapture = videoItem;

            String url = TQPictureUpload.downloadPath(videoItem.getCover());
            TQCaptureActivity.requestCapture(url, TQVideoSetsManager.this, 0x01);
        }

        @Override
        public void clickPlay(TQAtachModel videoItem) {
            if(videoItem == null || !videoItem.hasFile()){
                return;
            }

            String url = TQPictureUpload.downloadPath(videoItem.getFirstFile());
            TQPlayerActivity.showPlayer(url, TQVideoSetsManager.this);
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if(resultCode != RESULT_OK){
            return;
        }
        if(requestCode == 0x01 && currentCapture != null){
            String videoUrl = data.getStringExtra(TQCaptureActivity.RESULT_KEY);
            if(!StringHelper.isEmpty(videoUrl)){
                upload(videoUrl);
            }
        }
    }

    private void upload(final String file){
        TQUploadFileManager.Build lBuild = new TQUploadFileManager.Build()
                .setFile(file)
                .setListener(new IUploadFileListener() {
                    @Override
                    public void process(long current, long total) {
                        TQLoadingHelper.setText(String.format("进度%d%%", (int)(current/(total*1.0f) * 100)));
                    }

                    @Override
                    public void fail(String msg) {
                        TQLoadingHelper.setError(msg);
                    }

                    @Override
                    public void success(String filePath) {
                        TQLoadingHelper.dimiss();
                        modifyAttach(filePath);
                    }
                });

        TQLoadingHelper.build(this).setCancelable(false).show();
        lBuild.start();
    }
    private void modifyAttach(String file){
        currentCapture.replaceFile(file);


        TQLoadingHelper.build(this).setCancelable(false).show();
        currentCapture.doModifyAttach(new INetCallback() {
            @Override
            public void result(TQNetResult pNetResult) {
                if(!pNetResult.isSuccess()){
                    TQLoadingHelper.setError(pNetResult.getErrMsg());
                    return;
                }

                mTQVideoSetsAdapter.update(currentCapture);
                currentCapture = null;
                TQLoadingHelper.dimiss();
            }
        });
    }
}
